

using System;
using System.Text;
using ProjectCommon2.Interfaces;

namespace PolePosition.v3.EntityLibrary
{
	// No comments for the class here, all are in Entity.cs.cst
	/// <summary>
	/// </summary>
	/// <remarks></remarks>
	public sealed partial class VendorDistributionNotesXRecloserEntity
	{		
	#region " Contructors "
		/// <summary>Creates a new (empty) instance of <c>VendorDistributionNotesXRecloserEntity</c>. Use to create new VendorDistributionNotesXRecloser records.</summary>
		/// <remarks>This constructor just sets up an empty object; it does not contain any initialization code.</remarks>
		public VendorDistributionNotesXRecloserEntity()
		{
			// do nothing
		}
		/// <summary>Creates a new (empty) instance of <c>VendorDistributionNotesXRecloserEntity</c>. Use to create new VendorDistributionNotesXRecloser records.</summary>
		/// <param name="enableDefaults">If set to <c>true</c>, it ensures that default values are inserted for every
		/// <c>null</c> property before calculating an Entity's checksum and writing it to the database.
		/// See <see cref="EnableDefaults" /> for more information.</param>
		/// <remarks>Sets up an empty object and only initializes its <see cref="EnableDefaults" /> variable.</remarks>
		public VendorDistributionNotesXRecloserEntity(bool enableDefaults)
		{
			this.EnableDefaults = enableDefaults;
		}
		/// <summary>Creates a new instance of <c>VendorDistributionNotesXRecloserEntity</c>. Use when loading VendorDistributionNotesXRecloser records from the database.</summary>
		/// <remarks>Does not perform any work itself. Instead, it calls <see cref="LoadFromDataRow(System.Data.DataRow)" />
		/// to do the work.</remarks>
		/// <param name="dataRowToLoadFrom">Specifies the Data Row from which to obtain values for
		/// <c>VendorDistributionNotesXRecloserEntity</c>'s construction.</param>
		public VendorDistributionNotesXRecloserEntity(System.Data.DataRow dataRowToLoadFrom)
		{
			LoadFromDataRow(dataRowToLoadFrom, true);
		}
		/// <summary>Creates a new instance of <c>VendorDistributionNotesXRecloserEntity</c>. Use when loading VendorDistributionNotesXRecloser records from the database.</summary>
		/// <remarks>Does not perform any work itself. Instead, it calls <see cref="LoadFromDataRow(System.Data.DataRow, bool)" />
		/// to do the work.</remarks>
		/// <param name="dataRowToLoadFrom">Specifies the Data Row from which to obtain values for
		/// <c>VendorDistributionNotesXRecloserEntity</c>'s construction.</param>
		/// <param name="useCheckSum">Specifies if a checksum test should be performed to verify the integrity of the data.</param>
		public VendorDistributionNotesXRecloserEntity(System.Data.DataRow dataRowToLoadFrom, bool useCheckSum)
		{
			LoadFromDataRow(dataRowToLoadFrom, useCheckSum);
		}
	#endregion
	
		#region " Methods "

		/// <summary>
		/// Loads the properties of <c>VendorDistributionNotesXRecloserEntity</c> from the matching columns in the supplied Data Row,
		/// using checksum verification by default.
		/// </summary>
		/// <remarks>No work performed here. Calls <see cref="LoadFromDataRow(System.Data.DataRow, bool)" /> with
		/// <paramref name="useCheckSum" /> set to <c>true</c>.</remarks>
		/// <param name="sourceDataRow">Specifies the Data Row from which to obtain values for
		/// <c>VendorDistributionNotesXRecloserEntity</c>.</param>
		public void LoadFromDataRow(System.Data.DataRow sourceDataRow)
		{
			LoadFromDataRow(sourceDataRow, true);
		}

		/// <summary>
		/// Loads the properties of <c>VendorDistributionNotesXRecloserEntity</c> from the matching columns in the supplied Data Row. Provides the option
		/// to enable or disable checksum verification.
		/// </summary>
		/// <param name="sourceDataRow">Specifies the Data Row from which to obtain values for
		/// <c>VendorDistributionNotesXRecloserEntity</c>.</param>
		/// <param name="useCheckSum">Specifies if a checksum test should be performed to verify the integrity of the data.</param>
		///
		/// <exception cref="ProjectCommon2.Exceptions.MissingDataRowException">Thrown if either the Data Row 
		/// specified in <paramref name="sourceDataRow" /> or its associated table do not exist. The <c>throw</c>
		/// is performed by <see cref="ProjectCommon2.Helpers.ExceptionHelper.Throw(System.Exception)" />, which handles
		/// logging and other activities.</exception>
		///
		/// <exception cref="ProjectCommon2.Exceptions.MissingDataColumnException">Thrown if the Data Row specified in
		/// <paramref name="sourceDataRow" /> is missing one of its columns. The columns it is checked against were determined
		/// when this application was last compiled. The <c>throw</c> is performed by
		/// <see cref="ProjectCommon2.Helpers.ExceptionHelper.Throw(System.Exception)" />, which handles logging and other
		/// related activities.</exception>
		///
		///
		/// <remarks><para>Prior to loading the values from the given Data Row, this method checks that every column relevant to a VendorDistributionNotesXRecloser
		/// record is present in the given Data Row (the columns which should be present are specified in the source table used when building
		/// this file from its template). If there are columns missing, an exception is thrown as specified in the "Exceptions" section. 
		/// If not, it goes through every column in the given Data Row and assigns its value to the matching column property in the Entity, 
		/// except when the value from the database is <c>null</c> (this leaves the property helper object for that column property uninstantiated;
		/// see any of the column properties for more).</para>
		/// <para>After all values are loaded, the <see cref="AcceptChanges()" /> method is called to make each column
		/// property "save" its value and perform other actions that change <c>VendorDistributionNotesXRecloserEntity</c>'s status. See how the
		/// <c>AcceptChanges()</c> method works for more.</para>
		/// <para><b>Note</b>: There is a variation of this method that verifies the checksum value for tables that contain a checksum
		/// column. That method was generated at compile time only for those Entities that are supposed to deal with such tables.
		/// </para>
		///</remarks>
		public void LoadFromDataRow(System.Data.DataRow sourceDataRow, bool useCheckSum)
		{
			// passed in DataRow must exist
			if (sourceDataRow == null) 
			{
				ProjectCommon2.Helpers.ExceptionHelper.Throw(new ProjectCommon2.Exceptions.MissingDataRowException("VendorDistributionNotesXRecloserEntityGenBase.LoadFromDataRow() Data Row is null"));
			}
			// passed in DataRow must have associated DataTable
			if (sourceDataRow.Table == null) 
			{
				ProjectCommon2.Helpers.ExceptionHelper.Throw(new ProjectCommon2.Exceptions.MissingDataRowException("VendorDistributionNotesXRecloserEntityGenBase.LoadFromDataRow() DataRow.Table is null"));
			}
			
			// Vendor_Distribution_NotesXRecloser_Id
				
			if (!sourceDataRow.Table.Columns.Contains("Vendor_Distribution_NotesXRecloser_Id")) 
				{ProjectCommon2.Helpers.ExceptionHelper.Throw(new ProjectCommon2.Exceptions.MissingDataColumnException("Vendor_Distribution_NotesXRecloser_Id"));}
				
			if (sourceDataRow["Vendor_Distribution_NotesXRecloser_Id"] != System.DBNull.Value)
			{
				this.Vendor_Distribution_NotesXRecloser_IdObject.Value = (int?)int.Parse(sourceDataRow["Vendor_Distribution_NotesXRecloser_Id"].ToString());
			}
			
			// Vendor_Distribution_NoteFk
				
			if (!sourceDataRow.Table.Columns.Contains("Vendor_Distribution_NoteFk")) 
				{ProjectCommon2.Helpers.ExceptionHelper.Throw(new ProjectCommon2.Exceptions.MissingDataColumnException("Vendor_Distribution_NoteFk"));}
				
			if (sourceDataRow["Vendor_Distribution_NoteFk"] != System.DBNull.Value)
			{
				this.Vendor_Distribution_NoteFkObject.Value = (int?)int.Parse(sourceDataRow["Vendor_Distribution_NoteFk"].ToString());
			}
			
			// Vendor_Distribution_RecloserFk
				
			if (!sourceDataRow.Table.Columns.Contains("Vendor_Distribution_RecloserFk")) 
				{ProjectCommon2.Helpers.ExceptionHelper.Throw(new ProjectCommon2.Exceptions.MissingDataColumnException("Vendor_Distribution_RecloserFk"));}
				
			if (sourceDataRow["Vendor_Distribution_RecloserFk"] != System.DBNull.Value)
			{
				this.Vendor_Distribution_RecloserFkObject.Value = (int?)int.Parse(sourceDataRow["Vendor_Distribution_RecloserFk"].ToString());
			}
			
			if (!sourceDataRow.Table.Columns.Contains("FriendlyName")) 
			{ProjectCommon2.Helpers.ExceptionHelper.Throw(new ProjectCommon2.Exceptions.MissingDataColumnException("FriendlyName"));}
			this.FriendlyName = sourceDataRow["FriendlyName"].ToString();
			
			
			AcceptChanges();
		}
		
		
		/// <summary>
		/// Makes each column property save its current value, eliminating the possibility to revert to any previous values.
		/// This also has the effect of making the Entity "not dirty".
		/// </summary>
		/// <remarks>
		/// <para>All column properties of an Entity store two separate versions of their values. One version is the most current value, which may be 
		/// modified at will. The other version is the previous value assigned to the property. This makes it possible to revert to that previous value
		/// if and when needed (for more information about how this works, see the documentation for the
		/// <see cref="ProjectCommon2.Helpers.PropertyHelper&lt;PropertyDataType&gt;" /> class).
		/// </para>
		/// <para>When this method is called, it goes through every column property for <c>VendorDistributionNotesXRecloserEntity</c>
		/// 
		/// and calls their own <c>AcceptChanges()</c> method. This makes the two versions of the property's values equal, which means
		/// that the property cannot revert to a previous value anymore, thus effectively "saving" its value. Also, the Entity's 
		/// <see cref="IsMarkedToDelete" /> property is set to <c>false</c>, and <see cref="IsNew" /> to <c>null</c>. Finally, the 
		/// <see cref="CheckSumOriginal" /> property is set equal to the new <see cref="CheckSum" />.
		/// Besides preserving the Entity's values for a given point in time, these actions also change its status so that it is no longer
		/// "dirty" (see how the <see cref="IsDirty" /> property works).</para>
		/// 
		/// <para>Moreover, when a <c>VendorDistributionNotesXRecloserEntity</c> is first constructed from values in the database, calling this method is
		/// an acknowledgment that no values are missing from the record from which it was constructed,
		///  and that all values are of the expected type.
		/// </para>
		///</remarks>
		public void AcceptChanges()
		{
			// set is IsMarkedToDelete to false & set originals to current value
			IsMarkedToDelete = false;
			IsNew = null;
			this.Vendor_Distribution_NotesXRecloser_IdObject.AcceptChanges();
			this.Vendor_Distribution_NoteFkObject.AcceptChanges();
			this.Vendor_Distribution_RecloserFkObject.AcceptChanges();

			// updates the checksum original value
			CheckSumOriginal = CheckSum;
			RecurseVoidMethod(this, "AcceptChanges");
		}
		
		/// <summary>
		/// Makes <c>VendorDistributionNotesXRecloserEntity</c> reject the last changes to its column property values by reverting to the values from a previous point in time.
		/// </summary>
		/// <remarks>
		/// <para>Sets <c>VendorDistributionNotesXRecloserEntity</c>'s current values to either the original values from the database, if the <see cref="AcceptChanges()" />
		/// method has not been called since the Entity was first constructed, or to the last set of values saved 
		/// by the last call to <see cref="AcceptChanges()" />. It works by going through every one of <c>VendorDistributionNotesXRecloserEntity</c>'s
		/// properties 
		/// and calling their <c>CancelChanges()</c> method. It also sets the Entity's <see cref="IsMarkedToDelete" /> property to <c>false</c>.</para>
		/// <para>For more information about how this works, see the documentation for the <see cref="ProjectCommon2.Helpers.PropertyHelper&lt;PropertyDataType&gt;" />
		/// class and the <see cref="AcceptChanges()" /> method.</para>
		/// </remarks>
		public void RejectChanges()
		{
			// set is IsMarkedToDelete to false & set current values to original
			IsMarkedToDelete = false;
			this.Vendor_Distribution_NotesXRecloser_IdObject.CancelChanges();
			this.Vendor_Distribution_NoteFkObject.CancelChanges();
			this.Vendor_Distribution_RecloserFkObject.CancelChanges();
			RecurseVoidMethod(this, "RejectChanges");
		}
		
		/// <summary>
		/// Creates a copy of the current <c>VendorDistributionNotesXRecloserEntity</c> instance. Includes a copy of its Primary Key value by default.
		/// </summary>
		/// <remarks>
		/// No work performed here. Calls <see cref="Clone(bool)" /> with <paramref name="includeKeyField" /> set to <c>true</c>.
		/// </remarks>
		/// <returns>New instance of <c>VendorDistributionNotesXRecloserEntity</c>.</returns>
		public PolePosition.v3.EntityLibrary.VendorDistributionNotesXRecloserEntity Clone()
		{
			return Clone(true);
		}
		
		/// <summary>
		/// Creates a copy of the current <c>VendorDistributionNotesXRecloserEntity</c> instance.
		/// </summary>
		/// <param name="includeKeyField">Specifies whether or not the Primary Key value is to be copied.</param>
		/// <remarks>
		/// Creates a new <c>VendorDistributionNotesXRecloserEntity</c> by setting each of its properties equal to the corresponding property in the current
		/// <c>VendorDistributionNotesXRecloserEntity</c> instance. However, it skips the property corresponding to the Primary Key if
		/// <paramref name="includeKeyField" /> is set to <c>false</c>.
		/// </remarks>
		/// <returns>New instance of <c>VendorDistributionNotesXRecloserEntity</c>.</returns>
		public PolePosition.v3.EntityLibrary.VendorDistributionNotesXRecloserEntity Clone(bool includeKeyField)
		{
			PolePosition.v3.EntityLibrary.VendorDistributionNotesXRecloserEntity _c;
			_c = new PolePosition.v3.EntityLibrary.VendorDistributionNotesXRecloserEntity(); //create new Entity object
					// CS: If we are at a column that is PK
						if (includeKeyField) 
						{
						
						_c.Vendor_Distribution_NotesXRecloser_IdObject.Value = this.Vendor_Distribution_NotesXRecloser_Id;
					
						}
											
						_c.Vendor_Distribution_NoteFkObject.Value = this.Vendor_Distribution_NoteFk;
					
											
						_c.Vendor_Distribution_RecloserFkObject.Value = this.Vendor_Distribution_RecloserFk;
					
			return _c;
		}
		
		/// <summary>
		/// Compares the given VendorDistributionNotesXRecloserEntity object to the current VendorDistributionNotesXRecloserEntity instance.
		/// </summary>
		/// <remarks>No work performed here. Calls <see cref="IsEqual(object, bool)" /> with <paramref name="compareIsDirtyOnly" />
		/// set to <c>false</c>.
		/// </remarks>
		/// <param name="entityToTest">Specifies the <c>VendorDistributionNotesXRecloserEntity</c> instance to be tested.</param>
		/// <returns><c>true</c> if both instances are equal, <c>false</c> otherwise.</returns>
		public bool IsEqual(PolePosition.v3.EntityLibrary.VendorDistributionNotesXRecloserEntity entityToTest)
		
		{
			return IsEqual(entityToTest, false);
		}

		/// <summary>
		/// Compares the given VendorDistributionNotesXRecloserEntity object to the current VendorDistributionNotesXRecloserEntity instance, 
		/// providing the option to test only those properties in the given Entity that have been modified.
		/// </summary>
		///
		/// <param name="entityToTest">Specifies the <c>VendorDistributionNotesXRecloserEntity</c> instance to be tested.</param>
		/// <param name="compareIsDirtyOnly">Specifies that the comparison should be done on the given object's modified properties only.</param>
		///
		/// <returns><c>true</c> if both instances are equal, <c>false</c> otherwise.</returns>
		///
		/// <exception cref="System.InvalidCastException">Thrown if the given object is not of type VendorDistributionNotesXRecloserEntity.
		/// The <c>throw</c> is performed by <see cref="ProjectCommon2.Helpers.ExceptionHelper.Throw(System.Exception)" />,
		/// which handles logging and other related activities.
		/// </exception>
		///
		/// <exception cref="ProjectCommon2.Exceptions.IsDirtyNotTrueException">
		/// Thrown if the given object has no modified properties to test for.
		/// </exception>
		/// <remarks>
		/// Compares every property in the two objects, except Checksum, after making sure that the given object is of the right type.
		/// If <paramref name="compareIsDirtyOnly" /> is set to <c>true</c>, only those properties in the given Entity that have changed
		/// since load (their <c>IsDirty</c> attribute is <c>true</c>) will be used in the comparison process.
		/// </remarks>
		public bool IsEqual(object entityToTest, bool compareIsDirtyOnly)
		{
			if (entityToTest.GetType() != this.GetType())
			{
				string msg = "Expected type {0}, but got type {1}";
				msg = string.Format(msg, this.GetType().ToString(), entityToTest.GetType().ToString());
				ProjectCommon2.Helpers.ExceptionHelper.Throw(new System.InvalidCastException(msg));

			}
			
			PolePosition.v3.EntityLibrary.VendorDistributionNotesXRecloserEntity VendorDistributionNotesXRecloserToTest = (PolePosition.v3.EntityLibrary.VendorDistributionNotesXRecloserEntity)entityToTest; 
			
			//Cannot perform an isDirty comparison on an entity with no dirty properties
            if (!VendorDistributionNotesXRecloserToTest.IsDirty == true & compareIsDirtyOnly)
            {
                throw new ProjectCommon2.Exceptions.IsDirtyNotTrueException("Cannot perform an IsDirty comparison on an VendorDistributionNotesXRecloserEntity with no dirty properties");
            }
			
			// Vendor_Distribution_NotesXRecloser_Id
			if (compareIsDirtyOnly)
			{
				if 	(!object.Equals(VendorDistributionNotesXRecloserToTest.Vendor_Distribution_NotesXRecloser_IdObject.Value, this.Vendor_Distribution_NotesXRecloser_IdObject.Value) && VendorDistributionNotesXRecloserToTest.Vendor_Distribution_NotesXRecloser_IdObject.IsDirty == true)
				{return false;}
			}
			else
			{
				if 	(!object.Equals(VendorDistributionNotesXRecloserToTest.Vendor_Distribution_NotesXRecloser_IdObject.Value, this.Vendor_Distribution_NotesXRecloser_IdObject.Value))
				{return false;}
			}
			// Vendor_Distribution_NoteFk
			if (compareIsDirtyOnly)
			{
				if 	(!object.Equals(VendorDistributionNotesXRecloserToTest.Vendor_Distribution_NoteFkObject.Value, this.Vendor_Distribution_NoteFkObject.Value) && VendorDistributionNotesXRecloserToTest.Vendor_Distribution_NoteFkObject.IsDirty == true)
				{return false;}
			}
			else
			{
				if 	(!object.Equals(VendorDistributionNotesXRecloserToTest.Vendor_Distribution_NoteFkObject.Value, this.Vendor_Distribution_NoteFkObject.Value))
				{return false;}
			}
			// Vendor_Distribution_RecloserFk
			if (compareIsDirtyOnly)
			{
				if 	(!object.Equals(VendorDistributionNotesXRecloserToTest.Vendor_Distribution_RecloserFkObject.Value, this.Vendor_Distribution_RecloserFkObject.Value) && VendorDistributionNotesXRecloserToTest.Vendor_Distribution_RecloserFkObject.IsDirty == true)
				{return false;}
			}
			else
			{
				if 	(!object.Equals(VendorDistributionNotesXRecloserToTest.Vendor_Distribution_RecloserFkObject.Value, this.Vendor_Distribution_RecloserFkObject.Value))
				{return false;}
			}
			return true;
		}
		
		/// <summary>
		/// Returns a string that represents the current <c>VendorDistributionNotesXRecloserEntity</c> object.
		/// </summary>
		/// <remarks>Takes the Primary Key property of <c>VendorDistributionNotesXRecloserEntity</c> and converts it to a <c>string</c> using
		/// <see cref="System.Object.ToString()" />.
		/// </remarks>
		/// <returns>String conversion of <c>VendorDistributionNotesXRecloserEntity's</c> Primary Key property.</returns>
		public override string ToString()
		{
			return this.Vendor_Distribution_NotesXRecloser_Id.ToString();
		}

		/// <summary>
		/// Converts the current <c>VendorDistributionNotesXRecloserEntity</c> object to an XML document.
		/// </summary>
		///
		/// <returns>
		/// <see cref="System.Xml.XmlDocument" /> object that represents the current <c>VendorDistributionNotesXRecloserEntity</c>.
		/// </returns>
		///
		/// <remarks>
		/// Goes through everyone of <c>VendorDistributionNotesXRecloserEntity</c>'s column properties, and creates an XML element from its name.
		/// Then, it inserts the property's value into its corresponding XML element, and adds it to the document.
		/// </remarks>
		public System.Xml.XmlDocument ToXml()
		{
			System.Xml.XmlDocument _XDoc = new System.Xml.XmlDocument(); //create new XML doc
			_XDoc.LoadXml("<VendorDistributionNotesXRecloser />");
			System.Xml.XmlElement _XElement; 
			
			// Vendor_Distribution_NotesXRecloser_Id
			_XElement = _XDoc.CreateElement("Vendor_Distribution_NotesXRecloser_Id");
			_XElement.InnerText = this.Vendor_Distribution_NotesXRecloser_Id.ToString();
			_XDoc.DocumentElement.AppendChild(_XElement);
			
			// Vendor_Distribution_NoteFk
			_XElement = _XDoc.CreateElement("Vendor_Distribution_NoteFk");
			_XElement.InnerText = this.Vendor_Distribution_NoteFk.ToString();
			_XDoc.DocumentElement.AppendChild(_XElement);
			
			// Vendor_Distribution_RecloserFk
			_XElement = _XDoc.CreateElement("Vendor_Distribution_RecloserFk");
			_XElement.InnerText = this.Vendor_Distribution_RecloserFk.ToString();
			_XDoc.DocumentElement.AppendChild(_XElement);
			
			return _XDoc;
		}
		
        bool RecurseBooleanProperty(ICustomEntity entity, string propertyName)
        {
            /*
             * By: Jerry Nixon
             * On: November 13, 2007
             * To: Recurse all child entities (including lists) of a given boolean property 
             */

            // loop through entity properties

            foreach (System.Reflection.PropertyInfo _Property in entity.GetType().GetProperties())
            {
                // test for entity or entitylist types

                if (!typeof(ICustomEntity).IsAssignableFrom(_Property.PropertyType)
                    && !typeof(System.Collections.IEnumerable).IsAssignableFrom(_Property.PropertyType))
                    continue;

                // fetch value

                object _Value;
                _Value = _Property.GetValue(entity, null);

                // skip on null

                if (_Value == null)
                    continue;

                // process 

                System.Reflection.PropertyInfo _PropertyInfo;
                _PropertyInfo = _Property.PropertyType.GetProperty(propertyName);

                if (_PropertyInfo == null)
                    continue;

                if ((bool)_PropertyInfo.GetValue(_Value, null))
                    return true;
            }

            // finally return (false if void)

            return false;
        }

        void RecurseVoidMethod(ProjectCommon2.Interfaces.ICustomEntity entity, string methodName)
        {
            /*
             * By: Jerry Nixon
             * On: November 13, 2007
             * To: Recurse all child entities (including lists) of a given void method 
             */

            // loop through entity properties

            foreach (System.Reflection.PropertyInfo _Property in entity.GetType().GetProperties())
            {
                // test for entity or entitylist types

                if (!typeof(ICustomEntity).IsAssignableFrom(_Property.PropertyType) 
                    && !typeof(System.Collections.IEnumerable).IsAssignableFrom(_Property.PropertyType))
                    continue;

                // fetch value

                object _Value;
                _Value = _Property.GetValue(entity, null);

                // skip on null

                if (_Value == null)
                    continue;

                // process 
				System.Reflection.MemberInfo _MethodInfo = _Value.GetType().GetMethod(methodName);
				
				if(_MethodInfo == null)
					continue;

                _Property.PropertyType.InvokeMember(methodName,
                    System.Reflection.BindingFlags.InvokeMethod, null, _Value, null);
            }
        }

		#endregion
		
	}
}